package juc.semaphore;

import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;

/*jdk21虚拟线程*/
public class SemaphoreDemo {
    public static void main(String[] args) throws Exception {
        Semaphore semaphore = new Semaphore(1, true);
        semaphore.acquire();
        new Thread(() -> {
            try {
                System.out.println("t1 等待获取permit");
                semaphore.acquire();
                System.out.println("t1 获取permit");
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }finally {
                semaphore.release();
            }

        }).start();
        TimeUnit.SECONDS.sleep(2);
        System.out.println("main release permit");
        semaphore.release();

    }
}